package com.ruike.alisurface.services;

import android.app.IntentService;
import android.content.Intent;
import android.os.Environment;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.MyApplication;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.bean.advBean;
import com.voodoo.lib_frame.base.BaseView;
import com.voodoo.lib_frame.httpTools.DownloadUtil;
import com.voodoo.lib_utils.FileUtils;
import com.voodoo.lib_utils.L;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 活动类型广告图片下载类
 */
public class MyTopFileImgService extends IntentService implements BaseView {

    List<String> strpicList = new ArrayList<>();
    int mCount = 0;
    List<String> urllist = new ArrayList<>();
    String filepath  ;
    boolean isImgNext = true;
    public MyTopFileImgService() {
        super("MyFileImgService");
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        strpicList.clear();
        urllist.clear();
        mCount = 0;
        try {
            L.i("-----downImgFile-开始解析数据--Count-" + mCount);
            urllist = intent.getStringArrayListExtra("topadv");
            filepath = intent.getStringExtra("filepath");
            for (String str : urllist) {
                String picName = str.substring(str.lastIndexOf("/")).substring(1);
                strpicList.add(picName);
            }
            showImgList();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 下载 广告图片
     */
    public void showImgList() {
        doImgdownload();
    }


    /**
     * 进行图片的本地判断和下载
     */
    public void doImgdownload() {
        if (mCount >= urllist.size()) {
            L.i("-----downvdeioFile---下载图片 已经循环完了");
            return;
        }
        isImgNext = true;
        for (int i = 0; i < urllist.size(); i++) {
            if (isImgNext) {
                File myFile = new File(filepath, strpicList.get(mCount));
                if (!myFile.exists()) {
                    isImgNext = false;
                    downImgFile(urllist.get(mCount), 1 + "");
                } else {
                    isImgNext = true;
                    delFile(strpicList);
                    EventBus.getDefault().post(new MsgEventBus("topadv", strpicList.get(mCount)));
                }
                mCount++;
                if (mCount >= urllist.size()) {
                    L.i("-----downvdeioFile---下载图片 循环完了");
                    isImgNext = false;
                    return;
                }
            }
        }
    }


    //下载ship
    public void downImgFile(final String strNameList, String type) {
        try {
            L.i("-----downvdeioFile---活动图片下载成功");
            String Name = strNameList.substring(strNameList.lastIndexOf("/")).substring(1);
            DownloadUtil.getInstance().download(strNameList, filepath, Name, this);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void delFile(List<String> strpicList) {
        List<File> strlist = new ArrayList<>();
        strlist.clear();
        File[] folderFileList = FileUtils.getFolderFileList(filepath);
        L.i("tjf-活动图片集合--" + strpicList.toString());
        for (File file : folderFileList) {
            if (!strpicList.contains(file.getName())) {
                strlist.add(file);
            }
        }
        L.i("tjf--活动图片delect2222-" + strlist.toString());
        for (File file : strlist) {
            L.i(file.getName() + "-----活动图片 删除文件---");
            if (file.exists()) {
                file.delete();
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

    }


    @Override
    public void onStarted(String loadingStr) {

    }

    @Override
    public void onLoading(long total, long current) {

    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onErrorTip(String errorStr) {

    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {

    }

    @Override
    public void onFailure(String requestUrl, String requestMsg) {

    }

    @Override
    public void onError(String requestUrl, String errorMsg) {

    }

    @Override
    public void onError(String requestUrl, String errorMsg, String data) {

    }

    @Override
    public void onException(Exception exception) {

    }

    @Override
    public void onDownloadSuccess(String requestUrl, File file) {
        if (urllist.contains(requestUrl)) {
            L.i(mCount + "----活动图片下载成功 ----" + file.getAbsolutePath());
            delFile(strpicList);
            String picName = requestUrl.substring(requestUrl.lastIndexOf("/")).substring(1);
            EventBus.getDefault().post(new MsgEventBus("topadv",picName));
            doImgdownload();
        }
    }

    @Override
    public void onDownloading(String requestUrl, long total, long progress) {

    }

    @Override
    public void onDownloadFailed(String requestUrl, Exception e) {
        L.i(mCount + "---活动图片下载成功-Exception-requestUrl--" + requestUrl
                + "======" + e.toString());
        doImgdownload();
    }
}